$(function () {
    $("#regist").append("<li class=\"nav-item active\">\n" +
        "                    <a class='nav-link' href='subscribe.html'>预约 <span class='sr-only'>(current)</span></a>\n" +
        "                </li>")
    let user = sessionStorage.getItem("user");
    let parse = JSON.parse(user);
    if (parse===null){
        $("#user").append("<a class='nav-link' href='#' onclick='tologin()' style='color: white'>登录 </a>")
    }else if (parse.uRole==="用户"){
        $("#user").append("<div class='btn-group'>\n" +
            "  <button type='button' class='btn btn-secondary dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>\n" +
            "    个人管理\n" +
            "  </button>\n" +
            "  <div class='dropdown-menu dropdown-menu-right'>\n" +
            "    <button class='dropdown-item' type='button' onclick='toSubscribe()'>预约管理</button>\n" +
            "    <button class='dropdown-item' type='button' onclick='modifypwd()'>修改密码</button>\n" +
            "    <button class='dropdown-item' type='button' onclick='quit()'>退出登录</button>\n" +
            "  </div>\n" +
            "</div>")
    }else if (parse.uRole==="管理员"){
        $("#regist").empty();
        $("#user").append("<div class='btn-group'>\n" +
            "  <button type='button' class='btn btn-secondary dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>\n" +
            "    管理中心\n" +
            "  </button>\n" +
            "  <div class='dropdown-menu dropdown-menu-right'>\n" +
            "    <a href='admin.html'><button class='dropdown-item' type='button'>后台管理</button></a>\n" +
            "    <button class='dropdown-item' type='button' onclick='quit()'>退出登录</button>\n" +
            "  </div>\n" +
            "</div>")
    }


    layui.use('laypage', function(){
        var laypage = layui.laypage;
        //执行一个laypage实例
        $.post("/getCount",function (data) {
            laypage.render({
                elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                ,count: data //数据总数，从服务端得到
                ,jump: function(obj, first){
                    $.post("/getNotic",{"curr":obj.curr,"limit":obj.limit},function (data) {
                        let $notices = $("#notices");
                        $notices.empty();
                        for (let i = 0; i < data.length; i++) {
                            $notices.append(" <div class=\"col-lg-4\">\n" +
                                "                <h3>"+data[i].nTitle+"</h3>\n" +
                                "                <h4>"+data[i].nTime+"</h4>\n" +
                                "                <p>"+data[i].nContent+"</p>\n" +
                                "            </div><!-- /.col-lg-4 -->")
                        }
                    },'json')
                    //obj包含了当前分页的所有参数，比如：

                    //首次不执行
                    if(!first){
                        //do something
                    }
                }
            });
        },'json')
    });
})
function toSubscribe() {
    layui.use(['layer','form','laydate'], function(){
        let layer = layui.layer;
        let form = layui.form;
        let laydate = layui.laydate;
        layer.open({
            type: 1,
            // skin: 'layui-layer-rim', 加边框
            title: "预约管理",
            area: ['450px', '200px'],
            content: '<form class="layui-form" xmlns="http://www.w3.org/1999/html"> \n' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">预约时间</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" name="time" id="time" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '   <div class="layui-input-block">\n' +
                '<button class="layui-btn" lay-submit lay-filter="modify" type="button">修改时间</button>\n' +
                '<button class="layui-btn" lay-submit lay-filter="cancel" type="button">取消预约</button>\n' +
                '</div>' +
                '</div>' +
                '</form>',
            success: function () {
                let item = sessionStorage.getItem("user");
                let parse = JSON.parse(item);
                let uId = parse.uId;
                    form.render();
                    laydate.render({
                        elem: '#time' //指定元素
                    });
                    $.post("/getTime",function (data) {
                        let s = JSON.stringify(data);
                        window.sessionStorage.setItem("info",s);
                        let nowFormatDate1 = getNowFormatDate();
                        if (data.code===200&&data.sAppointmenttime>nowFormatDate1){
                            $("#time").val(data.sAppointmenttime);
                        }else {
                            $("#time").val("当前未预约");
                        }
                    },"json")
                form.on('submit(modify)',function (data) {
                    let item1 = sessionStorage.getItem("info");
                    let parse1 = JSON.parse(item1);
                    let sAppointmenttime = parse1.sAppointmenttime;
                    let nowFormatDate = getNowFormatDate();
                    let time = data.field.time;
                    if (sAppointmenttime===""&&sAppointmenttime<nowFormatDate){
                        layer.msg("当前未预约或预约时间已过")
                        return false;
                    }
                    if (nowFormatDate>time){
                        layer.msg("预约时间不能早于今日")
                        return false;
                    }else {
                        $.post("/modifyTime",{"modifyTime":time,"uId":uId},function (data) {
                            if (data.code===200){
                                layer.msg(data.msg)
                                window.location.href = "index.html"
                            }else {
                                layer.msg(data.msg)
                                return false;
                            }
                        },"json")
                    }
                })
                form.on('submit(cancel)',function (data) {
                    layer.open({
                        title: "预约管理",
                        content: '是否取消预约'
                        ,btn: ['确定', '否']
                        ,yes: function(index, layero){
                            $.post("/remove",{"uId":uId},function (data) {
                                if (data.code===200){
                                    layer.msg(data.msg)
                                    return false;
                                }else {
                                    layer.msg(data.msg)
                                    return false;
                                }
                            },"json")
                        }
                        ,btn2: function(index, layero){
                            //按钮【按钮二】的回调

                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                        ,cancel: function(){
                            //右上角关闭回调

                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                })
            }
        });
    });
}
//获取当前时间
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

function tologin() {
    layui.use(['layer','form'], function(){
        let layer = layui.layer;
        let form = layui.form;
        layer.open({
            type: 1,
            // skin: 'layui-layer-rim', 加边框
            title: "登录",
            area: ['450px', '300px'],
            content: '<form class="layui-form" xmlns="http://www.w3.org/1999/html"> \n' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">用户名</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" name="email" placeholder="请输入账号" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">密码</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="password" name="pwd" placeholder="请输入密码" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '  </div>' +
                '   <div class="layui-input-block">\n' +
                '<button class="layui-btn" lay-submit lay-filter="login" onclick="login()">登录</button></br>\n' +
                '<a href="register.html">没有账号？</a>' +
                '</div>' +
                '</div>' +
                '</form>'
        });
    });
}
//登录
function login() {
    layui.use(['form','layer'],function () {
        let form = layui.form;
        let layer = layui.layer;
        form.on('submit(login)', function(data){
            let email = data.field.email;
            let pwd = data.field.pwd;
            if (email!=="" && pwd!==""){
                $.post("/login",{"email":email,"pwd":pwd},function (data) {
                    if (data.code===500){
                        layer.msg(data.msg)
                        return false;
                    }else {
                        layer.msg("成功")
                        let s = JSON.stringify(data);
                        window.sessionStorage.setItem("user",s)
                        window.location.href="index.html"
                    }
                },"json")
            }else {
                layer.msg("账号密码不能为空")
                return false;
            }
        });
    })
}
//退出登录
function quit() {
    sessionStorage.removeItem("user")
    layui.use('layer', function() {
        let layer = layui.layer;
        let form = layui.form;
        $.post("/quit", function (data) {
            if (data.code === 200) {
                layer.msg(data.msg)
                window.location.href="index.html"
            }
        }, "json")
    })
}
//修改密码
function modifypwd() {
    layui.use(['layer','form'], function() {
        let layer = layui.layer;
        let form = layui.form;
        layer.open({
            type: 1,
            // skin: 'layui-layer-rim', 加边框
            title: "修改密码",
            area: ['450px', '380px'],
            content: '<form class="layui-form" xmlns="http://www.w3.org/1999/html"> \n' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">旧密码</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="password" id="oldpwd" name="oldpwd" placeholder="旧密码" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">新密码</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="password" id="newpwd" name="newpwd" placeholder="新密码" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '  </div>' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">确认新密码</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="password" id="oknewpwd" name="oknewpwd" placeholder="确认新密码" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '  </div>' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">验证码</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" id="code" name="code" placeholder="验证码" autocomplete="off" class="layui-input" style="width: 300px">\n' +
                '    </div>\n' +
                '  </div>' +
                '   <div class="layui-input-block">\n' +
                '<button class="layui-btn" type="button" onclick="getPwdCode()">获取验证码</button>' +
                '<button class="layui-btn"  onclick="determine()" type="button">修改</button></br>\n' +
                '</div>' +
                '</div>' +
                '</form>'
        })
    })
}
function getPwdCode() {
    let user = sessionStorage.getItem("user");
    let parse = JSON.parse(user);
    let uName = parse.uName;
    layui.use('layer', function() {
        let layer = layui.layer;
        layer.msg("邮件发送中...")
        $.post("/code",{"email":uName}, function (data) {
            layer.msg(data.msg)
        }, "json")
    })
}
//确认修改密码
function determine() {
    let oldpwd = $("#oldpwd").val();
    let newpwd = $("#newpwd").val();
    let oknewpwd = $("#oknewpwd").val();
    let code = $("#code").val();
    let user = sessionStorage.getItem("user");
    let parse = JSON.parse(user);
    layui.use(['layer','form'], function() {
        let layer = layui.layer;
        let form = layui.form;
        if (oldpwd!==parse.uPwd){
            layer.msg("原密码错误")
        } else if(newpwd!==oknewpwd){
            layer.msg("两次密码不一致")
        }else if (newpwd===""||oknewpwd===""){
            layer.msg("修改密码不能为空")
        }else if (newpwd.length<6){
            layer.msg("密码长度不能小于6位")
        }else {
            layer.open({
                title: "修改密码",
                content: '是否修改密码'
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    if (oldpwd===""||newpwd===""||oknewpwd===""||code===""){
                        layer.msg("不能为空")
                    }else {
                        //按钮【按钮一】的回调
                        $.post("/modifyPwd",{"newPwd":newpwd,"code":code},function (data) {
                            if (data.code===500){
                                layer.msg(data.msg)
                            }else if (data.code===200){
                                sessionStorage.removeItem("user")
                                window.location.href="index.html"
                            }
                        },"json")
                    }
                }
            });
        }
    })
}